#%%
# 生成用于演示的Whoose索引
# import 所需的包
import random
import pandas as pd
import numpy as np
from jieba.analyse.analyzer import ChineseAnalyzer
import os
from whoosh.index import create_in, open_dir, exists_in
from whoosh.fields import Schema, ID, TEXT, DATETIME
from datetime import datetime
print("import 所需包完成")

#%% 
# 定义生成的数据的结构
columns = ["ID", "title", "author", "date", "content","url","moreInfo_1", "moreInfo_2"]
demo_df = pd.DataFrame(columns=columns)

# 生成随机的数据
rows_num = 300
for i in range(300):
    demo_df = demo_df.append({
        "ID":"{}".format(i), 
        "title": "样例{}的一个标题".format(i),
        "author": "作者{}".format(random.randint(2, 20)),
        "date": "20{:0>2d}-{:0>2d}-{:0>2d} {:0>2d}:{:0>2d}:{:0>2d}".format(
            random.randint(0, 21),
            random.randint(1, 12),
            random.randint(1, 28),
            random.randint(0, 23),
            random.randint(0, 59),
            random.randint(0, 59)),
        "content": "。".join([ "今天第{}次听到有人说，我文章写的还不错".format(n) for n in range(random.randint(10, 30))]),
        "url": "/demo/not_exits_page_{}.html".format(i),
        "moreInfo_1": "demo_{}的更多信息1".format(i), 
        "moreInfo_2": "demo_{}的更多信息2".format(i)
    }, ignore_index=True)
print(demo_df.sample(5))
print(demo_df.shape) 
print("Demo数据生成完成")

#%%
# 使用jieba的中文分词工具
analyzer = ChineseAnalyzer()

# 定义Schema
# stored 决定是否会存在于查询结果中
schema = Schema(
    ID = ID(stored=False, unique=True),
    title=TEXT(stored=True, analyzer=analyzer),
    author=TEXT(stored=True, analyzer=analyzer),
    date=DATETIME(stored=True),
    content=TEXT(stored=True, analyzer=analyzer),
    url=TEXT(stored=True),
    moreInfo_1=TEXT(stored=True, analyzer=analyzer),
    moreInfo_2=TEXT(stored=True, analyzer=analyzer)
    )

ix_path = "demo_ix"
ix_name = "demo"
if not os.path.exists(ix_path):
    os.mkdir(ix_path)
ix = create_in(ix_path, schema, indexname=ix_name)

print("索引创建完成")

# %%
# 数据写入索引
try:
    writer = ix.writer()
except:
    writer.commit()
    print("LockError, 解除锁定")
    writer = ix.writer()

def update(row):
    writer.update_document(
        ID = row["ID"],
        title = row["title"],
        author = row["author"],
        date = datetime.strptime(row["date"], '%Y-%m-%d %H:%M:%S'),
        content = row["content"],
        url = row["url"],
        moreInfo_1 = row["moreInfo_1"],
        moreInfo_2 = row["moreInfo_2"]
        )        

    
demo_df.apply(update, axis=1)

writer.commit()

print("数据写入索引完成")
print("索引位于:{}文件夹中，索引名称为:{}".format(ix_path, ix_name))
print("可以使用以下命令打开索引:")
print("\tix = open_dir('{}', indexname='{}')".format(ix_path, ix_name))

# %%

# %%

# %%
